package com.xiaolaoban.user_center_back_end.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaolaoban.user_center_back_end.common.BaseResponse;
import com.xiaolaoban.user_center_back_end.common.ErrorCode;
import com.xiaolaoban.user_center_back_end.common.ResultUtils;
import com.xiaolaoban.user_center_back_end.model.User;
import com.xiaolaoban.user_center_back_end.model.request.UserLoginRequest;
import com.xiaolaoban.user_center_back_end.model.request.UserRegisterRequest;
import com.xiaolaoban.user_center_back_end.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

import static com.xiaolaoban.user_center_back_end.constant.UserConstant.ADMIN_USER;
import static com.xiaolaoban.user_center_back_end.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final String IS_NOT_ADMIN = "current user is not admin";
    private static final String PARAM_HAS_NULL_VALUE = "params have null value";
    @Resource
    UserService userService;

    @PostMapping("/userRegister")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            return ResultUtils.error(null);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return ResultUtils.error(ErrorCode.PARAM_ERROR);
        }
        return ResultUtils.success(userService.userRegister(userAccount, userPassword, checkPassword));

    }

    @PostMapping("/login")
    public BaseResponse<User> userRegister(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            return ResultUtils.error(null);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            return ResultUtils.error(ErrorCode.PARAM_ERROR);
        }
        return ResultUtils.success(userService.userLogin(userAccount, userPassword, request));

    }

    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request) {
        if (request == null) {
            return ResultUtils.error(ErrorCode.PARAM_NULL);
        }
        return ResultUtils.success(userService.userLogout(request));

    }

    @GetMapping("/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        User userObj = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (userObj == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN);
        }
        Long id = userObj.getId();
        User user = userService.getById(id);
        return ResultUtils.success(userService.getSafetyUser(user));
    }

    @GetMapping("/search")
    public BaseResponse<List<User>> userSearch(String username, HttpServletRequest request) {
        //鉴权，仅管理员可查
        if (!isAdmin(request)) {
            return ResultUtils.error(ErrorCode.NO_AUTH);
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(username)) {
            userQueryWrapper.like("username", username); //默认两边拼接%
        }
        //脱敏
        List<User> userList = userService.list(userQueryWrapper);
        List<User> list = userList.stream().map(user -> userService.getSafetyUser(user)).collect(Collectors.toList());
        return ResultUtils.success(list);


    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> userSearch(@RequestBody Long id, HttpServletRequest request) {
        //鉴权，仅管理员可删
        if (!isAdmin(request)) {
            return ResultUtils.error(ErrorCode.NO_AUTH);
        }
        if (id <= 0) {
            return ResultUtils.error(ErrorCode.PARAM_ERROR);
        }
        return ResultUtils.success(userService.removeById(id));

    }

    private boolean isAdmin(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        return user != null && user.getUserRole() == ADMIN_USER;
    }

}
